// pages/payPage/yanhuiPage/index.js
import Common from '../../../common/common.js';
let common = new Common;

Page({
  data: {
    region1: [],
    region: [],//地址选择器
    nowtime: '2020-01-01',//开始日期
    tradeFeastInfo: {
      banquetType: '',//宴会类型
      feastTime: '',//宴席日期
      feastBeginTime: '',//宴会开始时间
      feastEndTime: '',//宴会结束时间
      provinceId: '',
      province: '',
      cityId: '',
      city: '',
      countryId: '',
      country: '',
      address: '',//详细地址
      hotelAddress: '',//酒店名称
      tableNumber: '',//桌数
      goodsName: '',//商品名称
      quantity: '',//箱数
      contacts: '',//联系人
      contactsPhone: '',//联系方式
      deliverProvinceId: '',
      deliverProvince: '',//收货人省市区
      deliverCityId: '',
      deliverCity: '',
      deliverCountryId: '',
      deliverCountry: '',
      deliverAddress: ''
    }
  },
  onLoad: function (options) {
    let tradeFeastInfo = JSON.parse(options.tradeFeastInfo) || this.data.tradeFeastInfo;
    let region = [];
    if (tradeFeastInfo.province && tradeFeastInfo.city && tradeFeastInfo.country) {
      //二次进入
      region = [tradeFeastInfo.province, tradeFeastInfo.city, tradeFeastInfo.country];
    } else {
      region = JSON.parse(options.region)
    }

    let region1 = [];
    if (tradeFeastInfo.deliverProvince && tradeFeastInfo.deliverCity && tradeFeastInfo.deliverCountry) {
      //二次进入
      region1 = [tradeFeastInfo.deliverProvince, tradeFeastInfo.deliverCity, tradeFeastInfo.deliverCountry];
    } else {
      region1 = JSON.parse(options.region)
    }

    this.setData({
      tradeFeastInfo,
      region,
      region1,
      shopProvince: options.region ? JSON.parse(options.region)[0] : '',
      shopCity: options.region ? JSON.parse(options.region)[1] : '',
      islook: options.islook || 0
    });
    this.getNowData();
  },
  onShow: function () { },
  //选择宴席类型
  chooseBanquetType(e) {
    let banquetType = `tradeFeastInfo.banquetType`;
    this.setData({
      [banquetType]: e.currentTarget.dataset.type
    })
  },
  //宴席时间
  bindDateChange(e) {  //宴会活动日期
    console.log(e);
    let feastTime = `tradeFeastInfo.feastTime`;
    this.setData({
      [feastTime]: e.detail.value
    })
  },
  getNowData() {
    //当前时间的年月日往后推迟三天；
    let timestamp = Date.parse(new Date());
    let date = new Date(timestamp);
    //获取年份  
    let Y = date.getFullYear();
    //获取月份  
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日日期 
    let day = date.getDate() + 1;
    let D = day < 10 ? '0' + day : day;
    console.log("当前时间：" + Y + '年' + M + '月' + D + '日');
    // let hour = date.getHours();
    // let minute = date.getMinutes();
    // let second = date.getSeconds();
    this.setData({
      nowtime: Y + '-' + M + '-' + D
    })
  },
  //宴会开始时间
  bindFeastBeginTime(e) {
    console.log(e);
    //进行时间对比
    let feastBeginTime = `tradeFeastInfo.feastBeginTime`;
    if (!this.data.tradeFeastInfo.feastEndTime) {
      this.setData({
        [feastBeginTime]: e.detail.value
      })
    } else {
      let nowtime = this.data.nowtime.replace(/-/g, '/');
      let starttime = new Date(nowtime + ' ' + e.detail.value).getTime();
      let endtime = new Date(nowtime + ' ' + this.data.tradeFeastInfo.feastEndTime).getTime();
      if (starttime >= endtime) {
        wx.showToast({
          title: '开始时间不能大于结束时间！',
          icon: 'none'
        })
        return false
      } else {
        this.setData({
          [feastBeginTime]: e.detail.value
        })
      }
    }
  },
  //宴会结束时间
  bindFeastEndTime(e) {
    console.log(e);
    //进行时间对比
    let feastEndTime = `tradeFeastInfo.feastEndTime`;
    if (!this.data.tradeFeastInfo.feastBeginTime) {
      this.setData({
        [feastEndTime]: e.detail.value
      })
    } else {
      let nowtime = this.data.nowtime.replace(/-/g, '/');
      let starttime = new Date(nowtime + ' ' + this.data.tradeFeastInfo.feastBeginTime).getTime();
      let endtime = new Date(nowtime + ' ' + e.detail.value).getTime();
      if (starttime >= endtime) {
        wx.showToast({
          title: '开始时间不能大于结束时间！',
          icon: 'none'
        })
        return false
      } else {
        this.setData({
          [feastEndTime]: e.detail.value
        })
      }
    }
  },
  //宴席地址
  bindRegionChange(e) { //宴会地址选择
    let region = e.detail.value;
    this.setData({
      region,
    })
    this.queryFeastProvinceId(region, (res) => {
      let provinceId = `tradeFeastInfo.provinceId`;//省id
      let province = `tradeFeastInfo.province`;
      let cityId = `tradeFeastInfo.cityId`;//市id
      let city = `tradeFeastInfo.city`;//市
      let countryId = `tradeFeastInfo.countryId`;//区id
      let country = `tradeFeastInfo.country`;//区

      this.setData({
        [provinceId]: res.data.result.provinceId,
        [province]: res.data.result.province,
        [cityId]: res.data.result.cityId,
        [city]: res.data.result.city,
        [countryId]: res.data.result.countryId,
        [country]: res.data.result.country,
      })
    }, () => {
      wx.showToast({
        title: "地址选择出错,请稍后重试",
        icon: "none"
      })
    });

  },
  queryFeastProvinceId(region, success, err) {
    let data = {
      province: region[0],
      city: region[1],
      country: region[2] || ''
    }
    common.requestAjax('/api/trade-service/v1/trade/queryFeastProvinceId', 'POST', data).then(res => {
      if (res.data.code) {
        success && success(res);
      } else {
        err && err();
      }
    }).catch(error => {
      err && err();
      console.log(error);
    })
  },
  //填写地址
  inputAddress(e) {
    console.log(e);
    let address = `tradeFeastInfo.address`;
    this.setData({
      [address]: e.detail.value
    })
  },
  //填写酒店名称
  inputHotelAddress(e) {
    console.log(e);
    let hotelAddress = `tradeFeastInfo.hotelAddress`;
    this.setData({
      [hotelAddress]: e.detail.value
    })
  },
  //输入桌数
  inputTableNumber(e) {
    console.log(e);
    let tableNumber = `tradeFeastInfo.tableNumber`;
    if (e.detail.value && e.detail.value >= 5) {
      this.setData({
        [tableNumber]: e.detail.value
      })
    } else {
      wx.showToast({
        title: '必须大于5桌',
        icon: "none"
      })
      this.setData({
        [tableNumber]: 5
      })
    }

  },
  //输入联系人
  inputContacts(e) {
    console.log(e);
    let contacts = `tradeFeastInfo.contacts`;
    this.setData({
      [contacts]: e.detail.value
    })
  },
  //输入联系方式
  inputContactsPhone(e) {
    console.log(e);
    let contactsPhone = `tradeFeastInfo.contactsPhone`;
    let value = e.detail.value;
    if (value.length == 11 && value[0] == 1) {

    } else {
      wx.showToast({
        title: '请输入正确的手机号！',
        icon: 'none'
      })
      value = '';
    }
    this.setData({
      [contactsPhone]: value
    })
  },
  //收货地址省市区
  bindShRegionChange(e) { //宴会地址选择
    let region1 = e.detail.value;
    this.setData({
      region1,
    })
    this.queryFeastProvinceId(region1, (res) => {
      let deliverProvinceId = `tradeFeastInfo.deliverProvinceId`;//省id
      let deliverProvince = `tradeFeastInfo.deliverProvince`;
      let deliverCity = `tradeFeastInfo.deliverCity`;//市id
      let deliverCityId = `tradeFeastInfo.deliverCityId`;//市
      let deliverCountry = `tradeFeastInfo.deliverCountry`;//区id
      let deliverCountryId = `tradeFeastInfo.deliverCountryId`;//区

      this.setData({
        [deliverProvinceId]: res.data.result.provinceId,
        [deliverProvince]: res.data.result.province,
        [deliverCityId]: res.data.result.cityId,
        [deliverCity]: res.data.result.city,
        [deliverCountryId]: res.data.result.countryId,
        [deliverCountry]: res.data.result.country,
      })
    }, () => {
      wx.showToast({
        title: "地址选择出错,请稍后重试",
        icon: "none"
      })
    });
  },
  //收货人详细地址
  inputShAddress(e) {
    console.log(e);
    let deliverAddress = `tradeFeastInfo.deliverAddress`;
    this.setData({
      [deliverAddress]: e.detail.value
    })
  },
  saveBtn() {
    let tradeFeastInfo = this.data.tradeFeastInfo;
    let flag = true;
    let keyName = '';
    for (const key in tradeFeastInfo) {
      if (tradeFeastInfo.hasOwnProperty(key)) {
        const element = tradeFeastInfo[key];
        if (!element) {
          flag = false;
          keyName = key;
        }
      }
    }


    if (!flag) {
      if (keyName == 'countryId' || keyName == 'country') {
        wx.showToast({
          title: '请完善宴席地址地区信息！',
          icon: "none"
        })
        return false;
      } else if (keyName == 'deliverCountryId' || keyName == 'deliverCountry') {
        wx.showToast({
          title: '请完善收货地址地区信息！',
          icon: "none"
        })
        return false;
      } else {
        wx.showToast({
          title: '请完善宴席信息！',
          icon: "none"
        })
        return false;
      }
    }



    if (tradeFeastInfo.province != this.data.shopProvince || tradeFeastInfo.city != this.data.shopCity) {
      wx.showToast({
        title: '宴席地址与专卖店地址不在同一个城市,请修改宴席地址后再重新提交!',
        icon: 'none'
      })
      return false;
    }

    wx.showModal({
      title: "提示",
      content: "提交订单后将无法修改,是否保存?",
      success: res => {
        if (res.confirm) {
          wx.setStorageSync('tradeFeastInfo', tradeFeastInfo);
          wx.navigateBack()
        }
      }
    })
  }
})
